home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / amigalib / usleep.c < prev   
Encoding:
C/C++ Source or Header  |  1995-03-05  |  2.1 KB  |  95 lines

  1. /* This code is taken straight from the example in manual */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/io.h>
  5. #include <exec/memory.h>
  6. #include <devices/timer.h>
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10. #include <clib/alib_protos.h>
  11.  
  12. /* Our timer sub-routines */
  13. void delete_timer(struct timerequest *);
  14. void wait_for_timer(struct timerequest *, struct timeval *);
  15. struct timerequest *create_timer(ULONG);
  16.  
  17. void usleep(unsigned int);
  18.  
  19. struct Library *TimerBase;    /* to get at the time comparison functions */
  20.  
  21. struct timerequest *
  22. create_timer(ULONG unit)
  23. {
  24.   /* return a pointer to a timer request.  If any problem, return NULL */
  25.   LONG error;
  26.   struct MsgPort *timerport;
  27.   struct timerequest *TimerIO;
  28.  
  29.   timerport = CreatePort(0, 0);
  30.   if (timerport == NULL) {
  31.     return (NULL);
  32.   }
  33.   TimerIO = (struct timerequest *)
  34.     CreateExtIO(timerport, sizeof(struct timerequest));
  35.   if (TimerIO == NULL) {
  36.     DeletePort(timerport);    /* Delete message port */
  37.     return (NULL);
  38.   }
  39.   error = OpenDevice(TIMERNAME, unit, (struct IORequest *) TimerIO, 0L);
  40.   if (error != 0) {
  41.     delete_timer(TimerIO);
  42.     return (NULL);
  43.   }
  44.   return (TimerIO);
  45. }
  46.  
  47. /* more precise timer than AmigaDOS Delay() */
  48. void
  49. usleep(unsigned int tim)
  50. {
  51.   struct timeval tv;
  52.   struct timerequest *tr;
  53.  
  54.   tv.tv_secs = tim / 1000000;
  55.   tv.tv_micro = tim % 1000000;
  56.   /* get a pointer to an initialized timer request block */
  57.   tr = create_timer(UNIT_VBLANK);
  58.  
  59.   /* any nonzero return says usleep routine didn't work. */
  60.   if (tr == NULL) {
  61.     return;
  62.   }
  63.   wait_for_timer(tr, &tv);
  64.  
  65.   /* deallocate temporary structures */
  66.   delete_timer(tr);
  67.   return;
  68. }
  69.  
  70. void
  71. wait_for_timer(struct timerequest *tr, struct timeval *tv)
  72. {
  73.   tr->tr_node.io_Command = TR_ADDREQUEST;    /* add a new timer request */
  74.  
  75.   /* structure assignment */
  76.   tr->tr_time = *tv;
  77.  
  78.   /* post request to the timer -- will go to sleep till done */
  79.   DoIO((struct IORequest *) tr);
  80. }
  81.  
  82. void
  83. delete_timer(struct timerequest *tr)
  84. {
  85.   struct MsgPort *tp;
  86.   if (tr != 0) {
  87.     tp = tr->tr_node.io_Message.mn_ReplyPort;
  88.     if (tp != 0) {
  89.       DeletePort(tp);
  90.     }
  91.     CloseDevice((struct IORequest *) tr);
  92.     DeleteExtIO((struct IORequest *) tr);
  93.   }
  94. }
  95.